﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _13_ListView
{
    /// <summary>
    /// 不同的日志等级
    /// </summary>
    public enum LogLevel
    {
        /// <summary>
        /// 调试
        /// </summary>
        Debug,
        /// <summary>
        /// 消息
        /// </summary>
        Info,
        /// <summary>
        /// 错误
        /// </summary>
        Error,
        /// <summary>
        /// 警告
        /// </summary>
        Warn,
        /// <summary>
        /// 异常
        /// </summary>
        Fatal
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //增加表头内容：设置列名
            this.listView1.Columns.Add("日志等级", 120, HorizontalAlignment.Center);
            this.listView1.Columns.Add("时间", 200, HorizontalAlignment.Center);
            //设置消息列的宽度
            int infoWidth = this.listView1.Width - 120  -200;
            this.listView1.Columns.Add("消息", infoWidth, HorizontalAlignment.Left);

            //绑定事件
            this.button1.Click += (s,e)  => PrintLog("这是一条调试信息", LogLevel.Debug); 
            this.button2.Click += (s, e) => PrintLog("这是一条消息", LogLevel.Info);
            this.button3.Click += (s, e) => PrintLog("这是一条错误信息", LogLevel.Error);
            this.button4.Click += (s, e) => PrintLog("这是一条警告信息", LogLevel.Warn);
            this.button5.Click += (s, e) => PrintLog("这是一条异常信息", LogLevel.Fatal);

        }

        public void PrintLog(string info , LogLevel logLevel)
        {
            //listViewItem:列表视图控件中的项,专用于列表视图控件显示的数据
            ListViewItem listViewItem = new ListViewItem();
            listViewItem.Text = logLevel.ToString();//显示日志等级
            listViewItem.ImageIndex = (int)logLevel;//显示日志等级对应的图标
            //根据不同的等级，设置显示的前景颜色
            switch (logLevel)
            {
                case LogLevel.Debug:
                    listViewItem.ForeColor = Color.Black;
                    break;
                case LogLevel.Info:
                    listViewItem.ForeColor = Color.Blue;
                    break;
                case LogLevel.Error:
                    listViewItem.ForeColor = Color.Red;
                    break;
                case LogLevel.Warn:
                    listViewItem.ForeColor = Color.Orange;
                    break;
                case LogLevel.Fatal:
                    listViewItem.ForeColor = Color.DarkRed;
                    break;
                default:
                    break;
            }

            //添加时间
            listViewItem.SubItems.Add(DateTime.Now.ToString());

            //添加消息
            listViewItem.SubItems.Add(info);

            //将数据添加到控件中
            this.listView1.Items.Add(listViewItem);

            //确保最下方的内容可见
            this.listView1.EnsureVisible(this.listView1.Items.Count - 1);

        }

    }
}
